package com.rp.configure.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rp.common.utils.GetChanelById;
import com.rp.common.utils.NullIs;
import com.rp.configure.entity.Device;
import com.rp.configure.entity.DeviceAttribute;
import com.rp.configure.entity.vo.DeviceAttributeVo;
import com.rp.configure.entity.vo.SvgDevice;
import com.rp.configure.entity.vo.SvgVo;
import com.rp.configure.mapper.DeviceAttributeMapper;
import com.rp.configure.mapper.DeviceMapper;
import com.rp.configure.service.DeviceAttributeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rp.configure.service.DeviceService;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author vicente
 * @since 2021-07-16
 */
@Service
public class DeviceAttributeServiceImpl extends ServiceImpl<DeviceAttributeMapper, DeviceAttribute> implements DeviceAttributeService {

    @Autowired
    DeviceAttributeMapper deviceAttributeMapper;
    @Autowired
    DeviceMapper deviceMapper;

    @Override
    public void handleMessage(String gatewayId, byte[] message) {
        int slaveAddress = message[0];
        int code = message[1];
        int value = 0;
//        deviceAttributeMapper.updateByMessage(gatewayId,code,slaveAddress,value);
        List<DeviceAttributeVo> list = deviceAttributeMapper.getListByMessage(gatewayId, code, slaveAddress);
        if (!NullIs.isEmpty(list)) {
            switch (code) {
                case 2: {
                    list.forEach(deviceAttributeVo -> {
                        for (int i = 0; i < message[2]; i++) {
                            System.out.println(String.valueOf((byte) message[3]));
                            System.out.println((1 << deviceAttributeVo.getStartAddress()));
                            int i1 = (byte) message[3] & (byte) (1 << deviceAttributeVo.getStartAddress());
                            int i2 = (byte) i1 >> deviceAttributeVo.getStartAddress();
                            deviceAttributeVo.setValue(String.valueOf(i2));
                            DeviceAttribute deviceAttribute = new DeviceAttribute();
                            BeanUtils.copyProperties(deviceAttributeVo, deviceAttribute);
                            int i4 = deviceAttributeMapper.updateById(deviceAttribute);
                           if (i4>0){

                               SvgVo svgVo = deviceAttributeMapper.getOneState(deviceAttribute.getId());
                               ArrayList<SvgVo> svgVos = new ArrayList<SvgVo>();
                               svgVos.add(svgVo);
                               GetChanelById.bySvg(svgVo.getSubjectId()).forEach(channel -> {
                                   channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(svgVos)));
                               });
                               List<DeviceAttribute> id = getListByDeviceId(deviceAttribute.getDeviceId());
                            int j = 0;
                            Device device = new Device();
                            for (DeviceAttribute deviceAttribute1 : id
                            ) {
                                device.setId(deviceAttribute1.getDeviceId());
                               if (deviceAttribute1.getValue().equals("1")){
                                j = j + (int) Math.pow(2, deviceAttribute1.getRank());
                               }
                            }
                            device.setState(j);
                            device.setIsHandle(0);
                            deviceMapper.updateById(device);
                        }
                        }
                    });
                }
                case 3: {
                    for (int i = 0; i < message[2]; i++) {
                        value = value << (4 * i) + message[3 + i];
                    }
                }
                case 4: {
                    for (int i = 0; i < message[2]; i++) {
                        value = value << (4 * i) + message[3 + i];
                    }
                }
            }
        }
    }

    @Override
    public IPage<DeviceAttribute> listPage(Page<DeviceAttribute> objectPage, String deviceId) {
        return deviceAttributeMapper.listPage(objectPage, deviceId);
    }

    @Override
    public IPage<DeviceAttribute> listState(Page<DeviceAttribute> page, String userId, String subjectId) {
        return deviceAttributeMapper.listState(page, userId, subjectId);
    }

    @Override
    public List<DeviceAttribute> getListByDeviceId(String deviceId) {
        return deviceAttributeMapper.getListByDeviceId(deviceId);
    }

    @Override
    public List<SvgVo> getStateBySubject(String id) {
        return deviceAttributeMapper.getStateBySubject(id);
    }

    @Override
    public SvgDevice getBySubject(String subjectId, String svgId) {
        return deviceAttributeMapper.getBySubject(subjectId,svgId);
    }
}
